var count=0;
$("#single").click(function(){
	count=count+1;
	$.ajax({
		url:"single.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			var num=count;
			$("#questions").append($(res));
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+num+"题 单选";
			children[3].setAttribute("id",num+"p");
			
			children[3].onclick=function(){
				DeleteOnClock(parseInt(children[3].id),body);
			};
			
			$("#new").addClass("1");
			$("#new").attr("id",num);
			
			$("#question").attr("id",num+"q");
			
			$("#A").attr("id",num+"A");
			$("#B").attr("id",num+"B");
			$("#C").attr("id",num+"C");
			$("#D").attr("id",num+"D");
			
			var checks=document.getElementsByName("checks");
			for(var i=0;i<4;i++){
				checks[0].setAttribute("name",num+"answer");
			}
			
		}
	});		
});

$("#multiple").click(function(){
	count=count+1;
	$.ajax({
		url:"multiple.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			var num=count;
			$("#questions").append($(res));
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+count+"题  多选";
			
            children[3].setAttribute("id",num+"p");
			
			children[3].onclick=function(){	
				DeleteOnClock(parseInt(children[3].id),body);
			}
			
			$("#new").addClass("2");
			$("#new").attr("id",num);
			
            $("#question").attr("id",num+"q");
			
			$("#A").attr("id",num+"A");
			$("#B").attr("id",num+"B");
			$("#C").attr("id",num+"C");
			$("#D").attr("id",num+"D");
			
			var checks=document.getElementsByName("checks");
			for(var i=0;i<4;i++){
				checks[0].setAttribute("name",num+"answer");
			}
		
		}
	});		
});

$("#subject").click(function(){
	count=count+1;
	$.ajax({
		url:"subject.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			var num=count;
			$("#questions").append($(res));
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+count+"题  主观题";
			
            children[3].setAttribute("id",num+"p");
			
			children[3].onclick=function(){	
				DeleteOnClock(parseInt(children[3].id),body);			
			}
		
			$("#new").addClass("3");
			$("#new").attr("id",count);
			
			$("#question").attr("id",num+"q");
			$("#answer").attr("id",num+"a");
		}
	});		
});

function changeTestInf(number){
	$("#"+number+"q").attr("id",(number-1)+"q");//题干id
	
	$("#"+number+"A").attr("id",(number-1)+"A");//选项id
	$("#"+number+"B").attr("id",(number-1)+"B");
	$("#"+number+"C").attr("id",(number-1)+"C");
	$("#"+number+"D").attr("id",(number-1)+"D");
	
	var checks=document.getElementsByName(number+"answer");//radio列表
	for(var i=0;i<4;i++){
		checks[0].setAttribute("name",(number-1)+"answer");
	}
}

function DeleteOnClock(number,body){
	//console.log(body.id);
	body.remove();
	var type=0;
	var body_;
	var child;
	var testType;
	for(var i=number+1;i<=count;i++){
		console.log(i);
		body_=document.getElementById(i);
		type=body_.getAttribute("class");
		if(type==1){
			testType="单选";
		}else if(type==2){
			testType="多选";
		}else if(type==3){
			testType="主观题";
		}
		child=body_.childNodes;
		child[1].innerHTML="第"+(i-1)+"题 "+testType;
		child[3].setAttribute("id",(i-1)+"p");
		$("#"+i).attr("id",i-1);
		if(type==1||type==2){
			changeTestInf(i);
		}else if(type==3){
			$("#"+i+"q").attr("id",(i-1)+"q");
			$("#"+i+"a").attr("id",(i-1)+"a");
		}
		
	}
	count=count-1;
}

function GetRadioValue(RadioName){
    var obj;   
    obj=document.getElementsByName(RadioName);
    if(obj!=null){
        var i;
        for(i=0;i<obj.length;i++){
            if(obj[i].checked){
                return obj[i].value;           
            }
        }
    }else return null;
}

function GetCheckboxValue(RadioName){
    var obj;   
    var val=new Array();
    obj=document.getElementsByName(RadioName);
    if(obj!=null){
        var i;
        for(i=0;i<obj.length;i++){
            if(obj[i].checked){
            	val[i]=obj[i].value;        
            }
        }
        return val;
    }else return null;
}



$("#begin").click(function(){
	var testName=$("#testName").val();
	
	var test;
	var type;
	
	$.ajax({
		type:"post",
		url:"../createTest",
		data:{
			type:0,
			testName:testName,
		},
		success:function(data){
			
		}
	});   
	pauseTime(100);
	for(var i=1;i<=count;i++){
		test=document.getElementById(i);
		type=test.getAttribute("class");
		
		if(type==1){
			getSingleInf(i);
			pauseTime(100);
		}else if(type==2){
			getMultipleInf(i);
			pauseTime(100);
		}else if(type==3){
			getSubjectInf(i);
			pauseTime(100);
		}
	}	
});
//插入单选题
function getSingleInf(num){
	var topic=$("#"+num+"q").val();
	var A=$("#"+num+"A").val();
	var B=$("#"+num+"B").val();
	var C=$("#"+num+"C").val();
	var D=$("#"+num+"D").val();
	var answer=GetRadioValue(num+"answer");
	
	$.ajax({
		type:"post",
		url:"../createTest",
		data:{
			type:1,
			testNum:num,
			topic:topic,
			A:A,
			B:B,
			C:C,
			D:D,
			answer:answer,
		},
		success:function(data){
			console.log(data.code);
			
		}
	});   	
}
//插入多选题
function getMultipleInf(num){
	var topic=$("#"+num+"q").val();
	var A=$("#"+num+"A").val();
	var B=$("#"+num+"B").val();
	var C=$("#"+num+"C").val();
	var D=$("#"+num+"D").val();
	var answer=GetCheckboxValue(num+"answer");

	$.ajax({
		type:"post",
		url:"../createTest",
		data:{
			type:2,
			testNum:num,
			topic:topic,
			A:A,
			B:B,
			C:C,
			D:D,
			a_A:answer[0],
			a_B:answer[1],
			a_C:answer[2],
			a_D:answer[3],
		},
		success:function(data){
			console.log(data.code);
			
		}
	}); 
	
}
//插入主观题
function getSubjectInf(num){
	var topic=$("#"+num+"q").val();
	var answer=$("#"+num+"a").val();

	$.ajax({
		type:"post",
		url:"../createTest",
		data:{
			type:3,
			topic:topic,
			testNum:num,
			answer:answer,
		},
		success:function(data){
			console.log(data.code);	
			
		}
	}); 
	
	
}
//设置插入题目的时间间隔
function pauseTime(millTime) {
	var start=Date.now();
	while(true){
	var nowTime=Date.now();
	var offset=nowTime-start;
	if(offset>=millTime)
	break;
	}
}
//判断是否存在未填项
function allFinished(){
	var finished=true;
	var testName=$("#testName").val();
	if(test==null||test==""){
		finished=false;
	}
	for(var i=1;i<=count;i++){
		
	}
}